# coding : UTF-8
"""
作者：BingBO   时间：2022年10月23日
自动调整代码格式 ：Alt+Ctrl+L
"""
import cv2 as cv
import numpy as np
import moduleCommon as Com


def leftLaserExtract(src):
    print("********************** laserL2 左上光条特征提取 **********************")
    _, _, red = cv.split(src)
    _, binary = cv.threshold(red, 195, 255, cv.THRESH_BINARY)
    binary = cv.medianBlur(binary, 3)
    cv.imshow('binaryLeft', binary)
    cv.imwrite(r"F:\MVS_Data\binaryLeft.jpg", binary)
    print(binary.shape)
    hight, width = binary.shape

    # 检测边界点坐标
    gapMaxIdxS, gapMaxIdxL = Com.gapDetection(binary, 0, 0)
    d1 = gapMaxIdxL - gapMaxIdxS

    sumX, sumY = 0, 0
    for row in range(hight):  # 遍历这一行的每一列
        sumX += binary[row, gapMaxIdxS]
        sumY += binary[row, gapMaxIdxS] * (row + 1)  # 重心在第几列
    if sumX != 0:
        indexRow = (sumY / sumX) - 1  # 化为坐标索引
    edgeP1 = [round(indexRow), gapMaxIdxS]
    print("左上条纹的工件边缘特征点：", edgeP1)
    src[round(indexRow), gapMaxIdxS] = [255, 0, 0]

    # 计算每一列的光条数量
    for col in range(width):
        countUp = 0
        boundDn = []
        for row in range(hight - 1):
            diff = int(binary[row + 1, col]) - int(binary[row, col])  # 下一行像素值减去上一行像素值
            if diff > 0:
                countUp += 1
            if diff < 0 and countUp == 1:
                boundDn += [row]
            if countUp > 1 and binary[row, col] != 0:  # 将下面的重叠光条去掉
                binary[row, col] = 0
    cv.imshow('remove', binary)

    binaryLeft = binary[:, 0:int(gapMaxIdxS + d1 / 2)]
    gapMaxIdxS, gapMaxIdxL = Com.gapDetection(binaryLeft, 1, 0)

    # 水平COG  binary_Left
    sumx, sumy = 0, 0
    for col in range(width):  # 遍历这一行的每一列
        sumx += binary[gapMaxIdxS, col]
        sumy += binary[gapMaxIdxS, col] * (col + 1)  # 重心在第几列
    if sumx != 0:
        indexCol = (sumy / sumx) - 1  # 化为坐标索引
    featureP1 = [gapMaxIdxS, round(indexCol)]
    print("左上条纹的焊缝特征点：", featureP1)

    src[gapMaxIdxS, round(indexCol)] = [255, 0, 0]
    cv.imshow('featurePoint1', src)
    cv.imwrite(r"F:\MVS_Data\featurePoints_laserL1.png", src)
    return featureP1, edgeP1


def rightLaserExtract(src):
    print("********************** laserL1 右下光条特征提取 **********************")
    _, _, red = cv.split(src)
    _, binary = cv.threshold(red, 195, 255, cv.THRESH_BINARY)
    binary = cv.medianBlur(binary, 3)  # 中值滤波
    cv.imshow('binaryRight', binary)
    cv.imwrite(r"F:\MVS_Data\binaryRight.jpg", binary)
    hight, width = binary.shape

    leftIdx, rightIdx, gapLeft, gapRight = Com.gapDetection(binary, axis=0, mode=1)

    # 工件焊缝左半边图像（再横向进行COG）
    binaryLeft = binary[:, 0:int(leftIdx + gapLeft / 2)]
    binaryRight = binary[:, int(leftIdx + gapLeft / 2):int(rightIdx + gapRight / 2)]

    cv.imshow('binary_Left', binaryLeft)
    cv.imshow('binary_Right', binaryRight)

    # 垂直COG  binaryRight  求工件边界点
    sumx, sumy = 0, 0
    for row in range(hight):  # 遍历这一行的每一列
        sumx += binary[row, rightIdx]
        sumy += binary[row, rightIdx] * (row + 1)  # 重心在第几列
    if sumx != 0:
        indexRow = round(sumy / sumx) - 1  # 化为坐标索引
    edgeP2 = [indexRow, rightIdx]
    print("右下条纹的工件边界特征点：", edgeP2)
    src[indexRow, rightIdx] = [255, 0, 0]

    # 水平COG  binaryLeft   求焊缝特征点
    sumX = np.sum(binaryLeft, axis=1)  # 0列 1行
    # 求焊缝上最低点所在的行索引
    sumXIndex = np.where(sumX != 0)  # 返回索引numpy数组和元素类型的元组
    rowDownIdx = max(sumXIndex[0])

    sumx, sumy = 0, 0
    for col in range(binaryLeft.shape[1]):  # 遍历这一行的每一列
        sumx += binaryLeft[rowDownIdx, col]
        sumy += binaryLeft[rowDownIdx, col] * (col + 1)  # 重心在第几列
    if sumx != 0:
        indexCol = round((sumy / sumx) - 1)  # 化为坐标索引

    featureP2 = [rowDownIdx, indexCol]
    print("右下条纹的焊缝特征点：", featureP2)
    src[rowDownIdx, indexCol] = [255, 0, 0]

    cv.imshow('featurePoint2', src)
    cv.imwrite(r"F:\MVS_Data\featurePoints_laserL2.png", src)

    return featureP2, edgeP2
